//
//  ViewController.m
//  午分享_UIView中的坐标转换
//
//  Created by li cheng on 16/7/26.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIView *viewOfFather;
@property (nonatomic, strong) UIButton *buttonClick;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.viewOfFather = [[UIView alloc] initWithFrame:CGRectMake(40, 50, self.view.frame.size.width - 80, self.view.frame.size.height - 100)];
    self.viewOfFather.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.viewOfFather];
    self.buttonClick = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonClick.backgroundColor = [UIColor greenColor];
    self.buttonClick.frame = CGRectMake(100, 100, 100, 50);
    [self.buttonClick addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewOfFather addSubview:_buttonClick];
    
}
- (void)buttonClick:(UIButton *)button{
    // toView
    
    // 调用者是该Rect的父视图 View参数是你要转换的视图
    // 注意:
    // 1. .superView的写法
    // 2. 当view这个参数为nil的时候,默认转换为当前窗口,(屏幕)
    // 3. 当view不为空的时候,调用者和view的参数鼻血为同一Window对象
    CGRect newRect1 = [_buttonClick.superview convertRect:_buttonClick.frame toView:self.view];
    NSLog(@"%f %f %f %f", newRect1.origin.x, newRect1.origin.y, newRect1.size.width, newRect1.size.height);
    _buttonClick.frame = newRect1;
    [self.view addSubview:_buttonClick];
    
//    fromView
    // 这里的调用者与view参数相反
    CGRect newRect2 = [self.view convertRect:_buttonClick.frame fromView:_viewOfFather];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
